<?php
namespace Utuo\DistributionShopScore;

use Utuo\DistributionShopScore;
use Utuo\SystemInfo;

abstract class AbstractScore {
	
	protected $config = array(
	    /**
	     *  // 公共基础参数 
	     *  'cid' => 0, // 客户id *
	     *  'uid' => 0, // 用户id n 空时执行参数中配置
	     *  '...' => '...',
	     *  // 驱动参数配置
	     *  'tag' => '', // 驱动标签 *
	     *  'driverConfig' => array(), // 驱动参数配置重赋 n
	     */
	);
	
	/**
	 * 构造
	 * @param array $config 公共基础参数  + 驱动参数配置 见 $this->config *
	 */
	public function __construct( $config )  {

	    $storeConfig = M("DistributionShopScoreEvent")->where(array('cid'=>$config['cid'], 'tag'=>$config['tag']))->find();
	    /* 检查行为是否开启 */
	    if ( !$storeConfig['status'] ) {
	        E("未开启行为 {$storeConfig['tag']}", 100000);
	    }
	    
	    /* 存储的驱动配置信息 */
	    if ( !empty($storeConfig['config']) ) {
	        $storeConfig['config'] = unserialize($storeConfig['config']);
	    } else {
	        $storeConfig['config'] = array();
	    }
	    
	    /* 覆盖的驱动配置信息  */
	    if ( $config['driverConfig'] ) {
	        $config['driverConfig'] = array_merge($storeConfig['config'], $config['driverConfig']);
	    } else {
	        $config['driverConfig'] = $storeConfig['config']; // 默认配置
	    }
	    $this->config = $config;
	}
	
    /**
     * 执行积分行为
     * @param $execParams 执行参数
     * array(
     *    'uid' => 0, // 用户id  n 空时按公共基础参数配置
     *    'score' => 0, // 积分变化  n 空时按驱动配置
     * )
     * @return boolean
     */
    public function exec( $execParams = null ) {
       $execParams['uid'] = $this->uid;
       /* 驱动处理 */
       if ( $this->_exec( $execParams ) ) {
            /* 扩展积分变化记录 */
            method_exists( $this, "extendsRecored") and call_user_func(array($this, "extendsRecored"), array( $execParams ) );
            return true;
       } else {
           return false;
       }
    }
    
    /**
     * 用户当前积分信息
     */
    public function getUserScore( $uid = 0 ) {
        $uid = $uid ? $uid : $this->uid;
        $mScoreTable = M(DistributionShopScore::$config['score_table']);
        $userScore = $mScoreTable->where(array('cid'=>$this->cid, 'uid'=>$this->uid))->find();
        if ( !$userScore ) {
            $userScore = array(
                'cid' => $this->cid,
                'uid' => $uid,
                'total' => 0,
                'expend' => 0,
            );
            $userScore['id'] = $mScoreTable->add($userScore);
        }
        return $userScore;
    }
    
    /**
     * 获得用户可用积分
     * @param number $uid
     */
    public function getEnabledScore( $uid = 0 ) {
        $uid = $uid ? $uid : $this->uid;
        $userScore =  $this->getUserScore( $uid );
        $enabledScore = $userScore['total'] - $userScore['expend'];
        if ( $enabledScore > 0 ) {
            return $enabledScore;
        } else {
            return 0;
        }
    }
    
    /**
     * 改变用户积分
     * @param array $execParams 参见exec
     */
    protected function changeScore( $execParams ) {
        $mScoreTable = M(DistributionShopScore::$config['score_table']);
        /* 用户积分 */
        $userScore = $this->getUserScore( $execParams['uid'] );
        /* 积分更新 */
        $changeScore = $execParams['score']*1;
        $saveData = array();
        if ( $changeScore > 0 ) {
            $userScore['total'] = $saveData['total'] = $userScore['total'] + $changeScore;
        } else {
            $userScore['expend'] = $saveData['expend'] = $userScore['expend'] + (0 - $changeScore);
        }
        $mScoreTable->where(array('id'=>$userScore['id']))->save($saveData);
         
        /* 积分变化记录 */
        $this->baseRecored( $execParams, array('balance'=>$userScore['total']-$userScore['expend']) );
        return true;        
    }
    
	 /**
	  * 积分变化记录(主表记录)
	  * @param array $execParams 参见exec
	  * @param array $wparam
	  * array(
	  *    'balance' => 0, // 余额 *
	  * )
	  */
	 protected function baseRecored( $execParams, $wparam = array() ) {
	 	M("DistributionShopScoreRecord")->add(array(
	 	    'tag' => $this->tag,
		 	'cid' => $this->cid,
		 	'uid' => $execParams['uid'],
		 	'score' => $execParams['score'],
		 	'balance' => $wparam['balance']*1,
		 	'create_time' => time(),
	 	));
	 }
	 
	 /**
	  * 积分变化记录(从表记录)
	  * @param unknown $tag
	  * @param unknown $uid
	  * @param unknown $score
	  */
	 protected abstract function extendsRecored( $execParams );
	 
	 /**
	  * getter
	  * @param unknown $name
	  * @return multitype:
	  */
	 public function __get($name) {
	     return $this->config[$name];
	 }
	 
	 /**
	  * setter
	  * @param unknown $name
	  * @return multitype:
	  */
	 public function __set($name, $value) {
	     return $this->config[$name] = $value;
	 }
	 
}